﻿

//图片上传预览    IE是用了滤镜。
function previewImage(file) {
    if (!checkImgType(file)) { return; }
    if (!checkFileSize(file)) { return; }
    var fileval = $(file).val();
    var newname = NewGuid() + fileval.substr(fileval.lastIndexOf('.'));
    $(file).next().val(newname);
    var MAXWIDTH = 260;
    var MAXHEIGHT = 180;
    var div = $(file).nextAll("div[name='preview']");
    if (file.files && file.files[0]) {
        div.html('<img name=imghead>');
        var img = div.find('img[name=imghead]')[0];
        img.onload = function () {
            var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth, img.offsetHeight);
            img.width = rect.width;
            img.height = rect.height;
            // img.style.marginLeft = rect.left+'px';
            img.style.marginTop = rect.top + 'px';
        }
        var reader = new FileReader();
        reader.onload = function (evt) { img.src = evt.target.result; }
        reader.readAsDataURL(file.files[0]);
    }
    else //兼容IE
    {
        var sFilter = 'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src="';
        file.select();
        var src = document.selection.createRange().text;
        div.html('<img name=imghead>');
        var img = div.find('img[name=imghead]')[0];
        img.filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src = src;
        var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth, img.offsetHeight);
        status = ('rect:' + rect.top + ',' + rect.left + ',' + rect.width + ',' + rect.height);
        div.innerHTML = "<div name=divhead style='width:" + rect.width + "px;height:" + rect.height + "px;margin-top:" + rect.top + "px;" + sFilter + src + "\"'></div>";
    }
}

function clacImgZoomParam(maxWidth, maxHeight, width, height) {
    var param = { top: 0, left: 0, width: width, height: height };
    if (width > maxWidth || height > maxHeight) {
        rateWidth = width / maxWidth;
        rateHeight = height / maxHeight;

        if (rateWidth > rateHeight) {
            param.width = maxWidth;
            param.height = Math.round(height / rateWidth);
        } else {
            param.width = Math.round(width / rateHeight);
            param.height = maxHeight;
        }
    }

    param.left = Math.round((maxWidth - param.width) / 2);
    param.top = Math.round((maxHeight - param.height) / 2);
    return param;
}

/* 
 * 判断图片类型 
 *  
 * @param ths  
 *          type="file"的javascript对象 
 * @return true-符合要求,false-不符合 
 */
function checkImgType(ths) {
    if (ths.value == "") {
        alert("请上传图片");
        return false;
    } else {
        if (!/\.(gif|jpg|jpeg|png|GIF|JPG|PNG)$/.test(ths.value)) {
            alert("图片类型必须是.gif,jpeg,jpg,png中的一种");
            ths.value = "";
            return false;
        }

    }
    return true;
}

function checkFileSize(obj) {
    var objValue = obj.value;
    if (objValue == "") return;
    var fileLenth = -1;
    try {
        //对于IE判断要上传的文件的大小  
        var fso = new ActiveXObject("Scripting.FileSystemObject");
        fileLenth = parseInt(fso.getFile(objValue).size);
    } catch (e) {
        try {
            //对于非IE获得要上传文件的大小  
            fileLenth = parseInt(obj.files[0].size);
        } catch (e) {
            fileLenth = -1;
        }
    }

    if (fileLenth / 1024 > 500) {
        alert("图片过大，请上传小于500KB图片");
        return false;
    }

    return true;
}


/* 
 * 判断图片大小 
 *  
 * @param ths  
 *          type="file"的javascript对象 
 * @param width 
 *          需要符合的宽  
 * @param height 
 *          需要符合的高 
 * @return true-符合要求,false-不符合 
 */
function checkImgPX(ths, width, height) {
    var img = null;
    img = document.createElement("img");
    document.body.insertAdjacentElement("beforeEnd", img); // firefox不行  
    img.style.visibility = "hidden";
    img.src = ths.value;
    var imgwidth = img.offsetWidth;
    var imgheight = img.offsetHeight;

    alert(imgwidth + "," + imgheight);

    if (imgwidth != width || imgheight != height) {
        alert("图的尺寸应该是" + width + "x" + height);
        ths.value = "";
        return false;
    }
    return true;
}

//JS生成GUID函数,类似.net中的NewID();
function S4() {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}
function NewGuid() {
    return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
}